﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;
using System.Xml.Serialization;

namespace System.Data.DataSetGenerator
{
    /// <summary>
    /// Provides the description for a DataTable column
    /// </summary>
    [Serializable]
    [XmlType(TypeName = "DataColumn")]
    public class ColumnDescriptor
    {
        public ColumnDescriptor()
        {
            Util.InitializeObject(this);
        }

        /// <summary>
        /// Gets or sets a value that indicates whether null 
        /// values are allowed for this column
        /// </summary>
        [DefaultValue(true)]
        [XmlAttribute]
        public bool AllowDBNull { get; set; }

        /// <summary>
        /// Gets or sets the type name of the column
        /// </summary>
        [DefaultValue("System.String")]
        [XmlAttribute]
        public String DataType {get;set;}

        internal Type GetDataType()
        {
            if (String.IsNullOrEmpty(DataType))
                return typeof(String);
            return Type.GetType(DataType);
        }

        /// <summary>
        /// Gets or sets the caption for the column
        /// </summary>
        [XmlAttribute]
        [DefaultValue("")]
        public String Caption { get; set; }

        private String columnName = String.Empty;

        /// <summary>
        /// Gets or sets the name for the column
        /// </summary>
        [DefaultValue("")]
        [XmlAttribute]
        public String ColumnName 
        {
            get
            {
                return columnName;
            }
            set
            {
                columnName = value ?? "";
                if (String.IsNullOrEmpty(Caption))
                    Caption = columnName;
            }
        }

        /// <summary>
        /// Gets or sets the default value for the column
        /// </summary>
        [DefaultValue(null)]
        [XmlAttribute]
        public String DefaultValue { get; set; }

        /// <summary>
        /// Gets or sets the expression for the DataColumn
        /// </summary>
        [DefaultValue("")]
        [XmlAttribute]
        public String Expression { get; set; }

        /// <summary>
        /// Gets or sets a value that indicates whether the changes for the column are allowed
        /// </summary>
        [DefaultValue(false)]
        [XmlAttribute]
        public bool ReadOnly { get; set; }

        /// <summary>
        /// Gets or sets a value that indicates whether the values in each row of the column must be unique
        /// </summary>
        [DefaultValue(false)]
        [XmlAttribute]
        public bool Unique { get; set; }

        [DefaultValue(SchemaObjectActionEnum.Add)]
        [XmlAttribute]
        public SchemaObjectActionEnum Action { get; set; }
    }
}
